/**
 * Created with IntelliJ IDEA.
 * User: weixintao
 * Date: 14-12-11
 * Time: 下午4:35
 * To change this template use File | Settings | File Templates.
 */
package Hxw3DEngine.core.component.animation {
import ao3s2.Flash3dLib.CModule;

import Hxw3DEngine.core.materials.shader.SkinnedShader;

public class SkeletonClipCache {

    private var _jointSkinDatas:int;
    private var _jointPoseMatrices:int;
    private var _flags:Vector.<Boolean>;
    private var _frames:uint;
    private var _numJoints:uint;
    private var _jointPoseDatasLength:uint;
    private var _jointSkinMatricesLength:uint;

    public function SkeletonClipCache(frames:uint, numJoints:uint) {

        _frames = frames;
        _numJoints = numJoints;

        _jointSkinMatricesLength = numJoints * SkinnedShader._JointSkinDataLen;
        _jointPoseDatasLength = numJoints * SkinnedShader._JointPoseMatrixLen;

        _jointSkinDatas = CModule.malloc(frames * _jointSkinMatricesLength);
        _jointPoseMatrices = CModule.malloc(frames * _jointPoseDatasLength);

        _flags = new Vector.<Boolean>(frames);
        resetFlags();
    }

    public function dispose():void{
        CModule.free(_jointSkinDatas);
        _jointSkinDatas = 0;

        CModule.free(_jointPoseMatrices);
        _jointPoseMatrices = 0;
    }

    public function cacheFrame(frame:uint,matrices:int,poses:int):void{
        if(!_flags[frame]){
            Flash3dLib.copyFrame(_jointSkinDatas + frame * _jointSkinMatricesLength,_jointPoseMatrices + frame * _jointPoseDatasLength,matrices,poses,_jointSkinMatricesLength,_jointPoseDatasLength);
            _flags[frame] = true;
        }
    }
    public function getCachedFrame(frame:uint,matrices:int,poses:int):Boolean{
        if(_flags[frame]){
            Flash3dLib.copyFrame(matrices,poses,_jointSkinDatas + frame * _jointSkinMatricesLength,_jointPoseMatrices + frame * _jointPoseDatasLength,_jointSkinMatricesLength,_jointPoseDatasLength);
            return true;
        }
        return false;
    }
    public function resetFlags():void
    {
        for(var i:uint =0;i<_frames;i++){
            _flags[i] = false;
        }
    }
}
}
